package io.renren.modules.job.task;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.renren.modules.delay.DelayQueueManager;
import io.renren.modules.delay.DelayTask;
import io.renren.modules.delay.NotifyResponseTimeoutBase;
import io.renren.modules.netty.EqmContextMap;
import io.renren.modules.netty.XmlService;
import io.renren.util.HexUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
@Slf4j
public class HeartDataTask implements ITask {
  @Autowired
  private XmlService xmlService;
  @Autowired
  private DelayQueueManager delayQueueManager;
  @Override
  public void run(String params) {
    Collection<ChannelHandlerContext> values = EqmContextMap.getInstance().values();
    if (!values.isEmpty()) {
      values.forEach(e->{
        String heart = xmlService.heart();
        e.writeAndFlush(Unpooled.copiedBuffer(HexUtils.hexStr2Bytes(heart)));
        delayQueueManager.put(new DelayTask(new NotifyResponseTimeoutBase("notify"), 30 * 1000));
      });
    }
  }
}
